<html>
<head>
<title>Multi-Filehoster-Leech</title>
<link rel="stylesheet" type="text/css" href="style/style.css">
</head>

<body>
<script language="JavaScript" src="library/js/prototype.js"></script>
<script type="text/javascript" src="library/js/DownloadModel.js"></script>
<script type="text/javascript" src="library/js/SpeedMeter.js"></script>
<script type="text/javascript">

	var smeter = new SpeedMeter();

	var http = createRequestObject();
	var dlStarted = false;
	var dlComplete = false;
	var dlAborted = false;
	var dlLink;
	//var downloads[0].id;
	var debug = true;
	

	var activeDownload = null;
	var queue = new Array();
	var finishedDownloads = new Array();
	var failedDownloads = new Array();
	
	
	function addDownload(){
		var link = document.getElementById("linkinput").value;
		var dl = new DownloadModel(link);
		dl.progress = 0;

		if(activeDownload == null) {
			activeDownload = dl;
		}
		else{
			queue.push(dl);
		}

		displayTable();
		sendDownloadInfoRequest(link);
	}

	

	function createRequestObject() {
		var objAjax;
		var browser = navigator.appName;
		if(browser == "Microsoft Internet Explorer"){
			objAjax = new ActiveXObject("Microsoft.XMLHTTP");
		}else{
			objAjax = new XMLHttpRequest();
		}
		return objAjax;
	}
	

	function statusRequestLoop() {
		if(activeDownload.complete || activeDownload.aborted) {
			
			if(activeDownload.complete){
				finishedDownloads.push(activeDownload);
				sendFinishRequest();
			}

			if(queue.length != 0){
				activeDownload = queue.shift();
				sendDownloadRequest(activeDownload.link);
				displayTable();
				return;
			}
			else{
				activeDownload = null;
				displayTable();
				return;
			}
		}

		new Ajax.Updater(  
			{ failure: 'errors' },
			'download.php', 
			{
				method:     'post',
				parameters: { id: activeDownload.id, status: 1  },
				onSuccess:  function(transport){
							updateDownloadStatus(transport.responseText);
						}
			}
		);

		setTimeout('statusRequestLoop()',500);
	}

	function sendDownloadInfoRequest(link){		
		var params = "info=1&link="+link;
		
		http.open('POST','download.php');
		http.onreadystatechange = acceptAjaxMessage;

		//Send the proper header information along with the request
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");

		http.send(params);
	}

	function sendDownloadRequest(link){
		//document.downloadForm.submitButton.disabled = true;
		var params = "init=1&link="+link;
		
		http.open('POST','download.php');
		http.onreadystatechange = acceptAjaxMessage;

		//Send the proper header information along with the request
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");

		http.send(params);
	//	return false;
	}
	
	function sendFinishRequest(){
		var params = "fin=1&id="+activeDownload.id;
		
		http.open('POST','download.php');
		http.onreadystatechange = acceptAjaxMessage;

		//Send the proper header information along with the request
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");

		http.send(params);
		
	
	}

	function sendAbortRequest(){
		var params = "abort=1&id="+activeDownload.id;
		
		http.open('POST','download.php');
		http.onreadystatechange = acceptAjaxMessage;

		//Send the proper header information along with the request
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");

		http.send(params);
		
		dlAborted = true;
	
	}

	function updateDownloadStatus(response){
		var args = response.split("@");

		console.debug(response);

		if(args[0] == "suc" && !activeDownload.valuesSet){
			var tmp = args[4].split("/");
			activeDownload.valuesSet = true;
			activeDownload.link = tmp[tmp.length-1]; 
			
			if(args[3] > 1024) activeDownload.unit = "KB";
			if(args[3] > 1024000) activeDownload.unit = "MB";
			if(args[3] > 1024000000) activeDownload.unit = "GB";

			activeDownload.downloadSize = args[3];
			
			if(activeDownload.link.length <= 40)
				activeDownload.displayLink = activeDownload.link;
			else
				activeDownload.displayLink = "..."+activeDownload.link.substring(link.length-37,link.length-1);
		}
		
		if(args[0] == "suc"){
			activeDownload.progress =  Math.round((args[2]/args[3])*10000) / 100;
			activeDownload.downloaded = args[2];
			smeter.mesure(args[2]);				 
		}

		if(args[0] == "fin"){
			activeDownload.progress = 100;
			activeDownload.link = args[2];
			activeDownload.complete = true 
		}
		displayTable();	
	}


	function acceptAjaxMessage(){
		if(http.readyState == 4){
			var response = http.responseText;
			var args = response.split("@");
			
			if(args[0] == "info"){
				if(!queue.length){
					if(args[1] == "<404>"){
						failedDownloads.push(activeDownload);
						activeDownload = null;
						displayTable();
						return;
					}
					activeDownload.displayLink = args[1];
					sendDownloadRequest(args[2]);
				}
				else{
					for(var i=0; i<queue.length; i++){
						if(args[2] == queue[i].link){
							queue[i].displayLink = args[1];
						}
					}
				}
				return;
			}
			
			if(args[0] == "abort"){
				activeDownload.aborted = true;
			}
			
			if(args[0] == "id"){
				activeDownload.id = args[1];
				smeter = new SpeedMeter(4);
				statusRequestLoop();
			}
			
			displayTable();
		}
	}
	
	function selectAll(){
		var selectCount = 0;
		
		if(activeDownload == null && queue.length == 0)	return;
		
		if(activeDownload.selected) selectCount++; 
		activeDownload.selected= true;
		
		for(var i=0; i<queue.length; i++){
			if(queue[i].selected) selectCount++; 
			queue[i].selected = true;
		}
		
		if(selectCount == queue.length+1){
			activeDownload.selected = false;
			for(var i=0; i<queue.length; i++) queue[i].selected = false;
		}
		
		displayTable();
	}
	
	function displayTable(){
		if(activeDownload == null && queue.length == 0 && finishedDownloads.length == 0 && failedDownloads.length == 0){
			document.getElementById('activeDownloadsTableContent').innerHTML = "Nothing to do...";
			return;
		}
		
		var table = "<form name='statusForm'><table style='color:#FFFFFF'><tr style='color:#FFFFFF;'><td width=70>";
		table += "<center><button type='button' onmousedown='selectAll();'> All</button></center></td><td width=270>&nbsp;File";
		table += "</td><td width=165>&nbsp;Progress<td width=120>&nbsp;Speed</td><td width=122>Status</td>";
		var selected = "" ;
		
		if(activeDownload != null){
			if(activeDownload.selected) selected="checked";
			else selected="";
			var downloaded,downloadSize,progStr;
			
			if(activeDownload.unit == "Byte"){
				downloaded  = activeDownload.downloaded;
				downloaded  = activeDownload.downloaded;
			}
			if(activeDownload.unit == "KB"){
				downloaded  = (Math.round( (activeDownload.downloaded/1024) * 10 )) / 10;
				downloadSize  = (Math.round( (activeDownload.downloadSize/1024) * 10 )) / 10;
			}
			if(activeDownload.unit == "MB"){
				downloaded  = (Math.round( (activeDownload.downloaded/1024000) * 10 )) / 10;
				downloadSize  = (Math.round( (activeDownload.downloadSize/1024000) * 10 )) / 10;
			}
			if(activeDownload.unit == "GB"){
				downloaded  = (Math.round( (activeDownload.downloaded/1024000000) * 10 )) / 10;
				downloadSize  = (Math.round( (activeDownload.downloadSize/1024000000) * 10 )) / 10;
			}

			downloadSize != undefined ? progStr = downloaded + " / " + downloadSize + " " + activeDownload.unit : progStr = "";

			table += "<tr style='background-color:#4c4f4a'><td><div onmousedown='activeDownload.selected = !activeDownload.selected;'>";
			table += "<center><input type='checkbox'"+selected+"></center>"
			table += "<td>&nbsp;"+activeDownload.displayLink+"</td><td>&nbsp;"+progStr+"</td>";
			table += "<td>&nbsp;"+( smeter ? smeter.currSpeed : "initalizing...")+"</td><td>&nbsp;"+activeDownload.progress+" %</td></tr>";
		}
		for(var i=0; i<queue.length; i++){
			if(queue[i].selected) selected="checked";
			else selected="";
		
			var trstyle;
			if(i % 2 != 0) trstyle = "style='background-color:#4c4f4a;'";
			else trstyle = "";
			
			table += "<tr "+trstyle+"<td><div onmousedown='queue["+i+"].selected = !queue["+i+"].selected;'>";
			table += "<center><input type='checkbox'"+selected+"></center></div><td>&nbsp;"+queue[i].displayLink+"</td>";
			table += "<td></td><td></td><td style='color:#cdb21d;'>&nbsp;pending...</td></tr>";
		}

		for(var i=0; i<finishedDownloads.length; i++){
			var trstyle;
			var offset = 0;
			if(activeDownload != null) offset = 1;
			
			if((i+queue.length+offset-1) % 2 != 0) trstyle = "style='background-color:#4c4f4a;'";
			else trstyle = "";

			table += "<tr "+trstyle+"><td><center><input type='checkbox' disabled='true'></td><td>&nbsp;<a href='"+finishedDownloads[i].link+"'>";
			table += finishedDownloads[i].displayLink+"</a></td><td></td><td></td><td style='color:#66bd10;'>&nbsp;finished</td></tr>";
		}

		for(var i=0; i<failedDownloads.length; i++){
			var trstyle;
			var offset = 0;
			if(activeDownload != null) offset = 1;
			
			if((i+queue.length+offset-1) % 2 != 0) trstyle = "style='background-color:#4c4f4a;'";
			else trstyle = "";

			table += "<tr "+trstyle+"><td><center><input type='checkbox' disabled='true'></td><td>&nbsp;"+failedDownloads[i].link;
			table += "</td><td></td><td></td><td style='color:#ae2424;'>&nbsp;failed(404)</td></tr>";
		}
		
		table += "</table></form>";
		document.getElementById('activeDownloadsTableContent').innerHTML = table;
	}
		
	function resetForm(){
		document.downloadForm.submitButton.disabled = false;
		document.downloadForm.abortButton.disabled = true;
		document.getElementById('progressText').innerHTML = "";
		dlStarted = false;
		dlComplete = false;
		dlAborted = false;
		abortRequestSent = false;
	}
	
	function abortDownloads(){
		if(activeDownload.selected){
			sendAbortRequest();
		}
		
		for(var i=0; i<queue.length; i++){
			if(queue[i].selected){ 
				queue.splice(i,1);
				i--;
			}
			
		}
		
	}
	

	

</script>

<p id="debugText"> </p>
<div id="Content" style="padding:0px 15px;">

<p id="progressText1"> </p>
<br>
<div id="activeDownloadsTableHead">
<p style="margin-top:4px; margin-left:8px;"> ActiveDownloads: </p>
</div>
<div id="activeDownloadsTable">
<div id="activeDownloadsTableContent" style="padding-left:16px; padding-top:8px;">
Nothing to do...
</div>
</div>
<br>

<input type="text" id="linkinput" onkeydown="if (event.keyCode == 13) document.getElementById('submitButton').click();" value="rotnes.dk/carsten" size="40">
<table border="0">
<tr>
<td> <button id="submitButton" onClick="addDownload();">Send request</button> </td>
<td> <button name="abortButton" onClick="abortDownloads();" >Abort</button></td>
</tr>
</table>

<p id="progressText2"> </p>

</body>
</div>
</html>

